package com.lin.missyou.api.v1;

import com.github.dozermapper.core.DozerBeanMapperBuilder;
import com.github.dozermapper.core.Mapper;
import com.lin.missyou.exception.http.NotFoundException;
import com.lin.missyou.model.Theme;
import com.lin.missyou.service.ThemeService;
import com.lin.missyou.vo.ThemePureVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * 表与表的设计
 *  考虑清楚后，然后在考虑接口
 *  习惯阅读DDL
 *  熟练后使用DDL快，有一定编程的积累、实践
 *
 *  前端：
 *
 *
 * options 意义
 *      更加简单的代码
 *      标准写法
 *      强制考虑 空值情况
 * @author gcq
 * @Create 2021-10-13
 */
@RestController
@RequestMapping("/theme")
public class ThemeController {

    @Autowired
    private ThemeService themeService;

    @GetMapping("/by/names")
    public List<ThemePureVo> getThemeGroupByNames(@RequestParam("names") String names)
    {
        List<String> nameList = Arrays.asList(names.split(","));
        List<Theme> themes = themeService.findByNames(nameList);
        List<ThemePureVo> list = new ArrayList<>();
        themes.forEach(theme -> {
            Mapper mapper = DozerBeanMapperBuilder.buildDefault();
            ThemePureVo vo = mapper.map(theme, ThemePureVo.class);
            list.add(vo);
        });
        return list;
    }

    @GetMapping("/name/{name}/with_spu")
    public Theme getThemeByNameWithSpu(@PathVariable("name") String name)
    {
        Optional<Theme> optionalTheme = this.themeService.findByName(name);
        return optionalTheme.orElseThrow(() -> new NotFoundException(30003));
    }
}